package com.andaily.web.controller.web.validator;

import com.andaily.domain.dto.cooking.CookingAccountDto;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * @author Shengzhao Li
 */
public class CookingAccountValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return CookingAccountDto.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors, "creatorGuid", null, "记录人不能为空");

        CookingAccountDto accountDto = (CookingAccountDto) target;
        if (!NumberUtils.isNumber(accountDto.getAmount())) {
            errors.rejectValue("amount", null, "金额必须为数字");
        }
    }
}
